package com.bw.demo.controller;

import java.util.Set;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;

import org.hibernate.validator.HibernateValidator;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.bw.demo.model.BwResult;
import com.bw.demo.model.IdsReq;
import com.bw.demo.model.UserGroupDto;
import com.bw.demo.valid.group.ValidationGroup.AddGroup;
import com.bw.demo.valid.group.ValidationGroup.EditGroup;
import com.bw.demo.valid.group.ValidationGroup.IdGroup;
import com.bw.demo.valid.group.ValidationGroup.SelectGroup;

/**
 * 
 */
@Validated
@RestController
@RequestMapping("/test/group")
public class UserGroupController 
{
	
	/********************  方式一：纯注解模式  *************************/
	
	/**
	 * 添加分组
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "save", method = RequestMethod.POST)
	public BwResult save(@Validated(AddGroup.class) @RequestBody UserGroupDto user) 
	{
		return BwResult.success().setData(user);
	}
	
	@RequestMapping(value = "update", method = RequestMethod.GET)
	public BwResult update(@Validated(EditGroup.class) UserGroupDto user) 
	{
		return BwResult.success().setData(user);
	}
	
	@RequestMapping(value = "delete", method = RequestMethod.GET)
	public BwResult delete(@Validated(IdGroup.class) UserGroupDto user) 
	{
		return BwResult.success().setData(user);
	}
	
	@RequestMapping(value = "batchDelete", method = RequestMethod.POST)
	public BwResult batchDelete(@Validated @RequestBody IdsReq ids) 
	{
		return BwResult.success().setData(ids);
	}
	
	@RequestMapping(value = "get", method = RequestMethod.GET)
	public BwResult get(@Validated(SelectGroup.class) UserGroupDto user) 
	{
		return BwResult.success().setData(user);
	}
	
	/**************************  方式二：BindingResult接收校验结果  ***********************/
	
	@RequestMapping(value = "save2", method = RequestMethod.POST)
	public BwResult save2(@Validated(AddGroup.class) @RequestBody UserGroupDto user, BindingResult result) 
	{
		if (result.hasErrors()) 
		{
			for (ObjectError error : result.getAllErrors()) {
				System.out.println(error.getDefaultMessage());
			}
			throw new RuntimeException("参数校验失败");
		}
		return BwResult.success().setData(user);
	}
	
	/*************************  方式三：手动校验模式  ************************/
	
	@RequestMapping(value = "save3", method = RequestMethod.POST)
	public BwResult save3(@RequestBody UserGroupDto user) 
	{
		// 构建校验器， failFast-true:快速失败
		Validator validator = Validation.byProvider(HibernateValidator.class).configure()
			.failFast(false).buildValidatorFactory().getValidator();
		// 校验
		Set<ConstraintViolation<UserGroupDto>> validResult = validator.validate(user, AddGroup.class);
		if (validResult.size() > 0) 
		{
			throw new RuntimeException("参数校验失败");
			/*
			 * for (ConstraintViolation<UserDto> constraintViolation : validResult) 
			 * { 
			 * 	String message = constraintViolation.getMessage();
			 * }
			 */
		}
		return BwResult.success().setData(user);
	}
	
}
